package com.nineclock.system.controller;

import com.nineclock.attendance.dto.DepartmentOrUserSimpleDTO;
import com.nineclock.common.entity.PageResult;
import com.nineclock.common.entity.Result;
import com.nineclock.system.dto.SysCompanyUserDTO;
import com.nineclock.api.system.OrganizationControllerApi;
import com.nineclock.system.dto.SysDepartmentDTO;
import com.nineclock.system.service.SysCompanyUserService;
import com.nineclock.system.service.SysDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
@RestController
@RequestMapping("/organization")
public class OrganizationController implements OrganizationControllerApi {

    @Autowired
    SysCompanyUserService sysCompanyUserService;
    @Autowired
    SysDepartmentService departmentService;



    @Override
    @GetMapping("/members/simple")
    public Result<List<SysCompanyUserDTO>> queryCompanyMemberList() {

        List<SysCompanyUserDTO> companyUserDTOList = sysCompanyUserService.queryCompanyMemberList();

        return Result.success(companyUserDTOList);
    }

    @Override
    @GetMapping("/department")
    public Result<List<SysDepartmentDTO>> queryDepartmentList() {
        List<SysDepartmentDTO> departmentDTOList = departmentService.queryDepartmentList();
        return Result.success(departmentDTOList);
    }

    @Override
    @GetMapping("/members")
    public Result<PageResult<SysCompanyUserDTO>> members(
           @RequestParam(value = "page", defaultValue = "1") Integer page,
           @RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize,
            String keyword,
            Long departmentId) {
        PageResult<SysCompanyUserDTO> pageResult = sysCompanyUserService.members(page,pageSize,keyword,departmentId);
        return Result.success(pageResult);
    }

    @Override
    @PostMapping("/uploadExcel")
    public Result uploadExcel(MultipartFile excelFile) throws IOException {
        sysCompanyUserService.importDate(excelFile);
        return null;
    }

    @Override
    @GetMapping("/department/top")
    public Result<List<Long>> queryDepartmentIds() {
        List<Long> ids =  departmentService.queryDepartmentIds();
        return Result.success(ids);
    }

    @Override
    @GetMapping("/department/membernum")  //list集合必须要加@RequestParam 否则接受不到
    public Result<Integer> queryUserCountByDepartmentIds(@RequestParam("departmentIds") List<Long> departmentIds) {
        Integer count =   sysCompanyUserService.queryUserCountByDepartmentIds(departmentIds);
        return Result.success(count);
    }

    @Override
    @GetMapping("/simple")
    public Result<List<DepartmentOrUserSimpleDTO>> querySimpleDepartment(@RequestParam(value = "id",defaultValue = "0")
                                                                                     Long departmentId, Integer includeMember) {

        List<DepartmentOrUserSimpleDTO> departmentOrUserSimpleDTOS = sysCompanyUserService.querySimpleDepartment(departmentId,includeMember);
        return Result.success(departmentOrUserSimpleDTOS);
    }
}
